package com.deity.news.dao;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//import net.hongliang.util.HibernateUtil;
//import org.hibernate.Query;
//import org.hibernate.Session;
//import org.hibernate.Transaction;
//import com.deity.news.bean.Document;
//import com.deity.news.bean.Document;
import com.deity.news.bean.UploadFile;

import com.deity.news.dao.DBConnect;
//import com.deity.news.bean.Category;
//import com.deity.news.bean.Document;

public class TempUploadFileDAO {

	private static void fill(ResultSet rs, UploadFile uploadFile)
			throws SQLException {
		uploadFile.setId(rs.getInt("id"));
		uploadFile.setName(rs.getString("name"));
		uploadFile.setUrlPath(rs.getString("urlPath"));
		uploadFile.setRealPath(rs.getString("realPath"));
		uploadFile.setUploadUser(rs.getString("uploadUser"));
		uploadFile.setUploadDate(rs.getTimestamp("uploadDate"));
		uploadFile.setRemark(rs.getString("remark"));
		uploadFile.setHomepageVideo(rs.getBoolean("isHomepageVideo"));
	}

	// public static int amountAll() {
	// int amount = 0;
	//
	// Session session = HibernateUtil.currentSession();
	// Transaction tx = session.beginTransaction();
	//
	// Query q = session.createQuery("select count(*) from UploadFile d");
	//
	// amount = ((Integer) q.uniqueResult()).intValue();
	//
	// tx.commit();
	//
	// return amount;
	// }

	// public static List find(int start, int size) {
	// List list = null;
	//
	// Session session = HibernateUtil.currentSession();
	// Transaction tx = session.beginTransaction();
	//
	// Query q =
	// session.createQuery("from UploadFile d order by d.uploadDate desc");
	// if (size > 0) {
	// q.setFirstResult(start);
	// q.setMaxResults(size);
	// }
	// list = q.list();
	//
	// tx.commit();
	//
	// return list;
	// }

	// public static UploadFile findById(int fid) {
	// UploadFile uf = null;
	//
	// Session session = HibernateUtil.currentSession();
	// Transaction tx = session.beginTransaction();
	//
	// Query q = session.createQuery("from UploadFile d where d.id=:fid");
	// q.setInteger("fid", fid);
	// uf = (UploadFile)q.uniqueResult();
	// tx.commit();
	//
	// return uf;
	// }
	//
	// public static void remove(int fid) {
	// HibernateUtil.remove(findById(fid));
	// }

	public static int amountAll() {
		int amount = 0;
		DBConnect dbc = null;
		String sql = "select count(*) from tb_upload";
		// Document document = null;
		try {
			dbc = new DBConnect(sql);
			// dbc.setInt(1, cid);
			ResultSet rs = dbc.executeQuery();
			if (rs.next()) {
				// document = new Document();
				amount = rs.getInt(1);

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (dbc != null)
					dbc.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return amount;
	}
	
	public static int amountVideoFiles() {
		int amount = 0;
		DBConnect dbc = null;
		String sql = "select count(*) from tb_upload where isHomepageVideo = 1";
		// Document document = null;
		try {
			dbc = new DBConnect(sql);
			// dbc.setInt(1, cid);
			ResultSet rs = dbc.executeQuery();
			if (rs.next()) {
				// document = new Document();
				amount = rs.getInt(1);

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (dbc != null)
					dbc.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return amount;
	}
	
	/* oracle dao
	public static List find(int start, int size) {

		DBConnect dbc = null;
		String sql = "select * from (select a.*, rownum rn from (select * from tb_upload order by uploadDate desc) a where rownum <= ?)where rn > ?";
		List<UploadFile> list = new ArrayList<UploadFile>();
		try {
			dbc = new DBConnect(sql);
			dbc.setInt(1, start + size);
			dbc.setInt(2, start);
			ResultSet rs = dbc.executeQuery();
			while (rs.next()) {
				UploadFile uploadFile = new UploadFile();
				fill(rs, uploadFile);
				list.add(uploadFile);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (dbc != null)
					dbc.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;

	}
	*/
	// mysql dao
	public static List<UploadFile> find(int start, int size) {

		DBConnect dbc = null;
		String sql = "select * from tb_upload order by uploadDate desc limit ? , ?";
		List<UploadFile> list = new ArrayList<UploadFile>();
		try {
			dbc = new DBConnect(sql);
			dbc.setInt(1, start);
			dbc.setInt(2, size);
			ResultSet rs = dbc.executeQuery();
			while (rs.next()) {
				UploadFile uploadFile = new UploadFile();
				fill(rs, uploadFile);
				list.add(uploadFile);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (dbc != null)
					dbc.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;

	}
	
	// mysql dao
	public static List<UploadFile> findVideoFiles(int start, int size) {

		DBConnect dbc = null;
		String sql = "select * from tb_upload where isHomepageVideo = 1 order by uploadDate desc limit ? , ?";
		List<UploadFile> list = new ArrayList<UploadFile>();
		try {
			dbc = new DBConnect(sql);
			dbc.setInt(1, start);
			dbc.setInt(2, size);
			ResultSet rs = dbc.executeQuery();
			while (rs.next()) {
				UploadFile uploadFile = new UploadFile();
				fill(rs, uploadFile);
				list.add(uploadFile);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (dbc != null)
					dbc.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;

	}
	

	public static UploadFile findById(int id) {
		DBConnect dbc = null;
		String sql = "select * from tb_upload where id=?";
		UploadFile uploadFile = null;
		try {
			dbc = new DBConnect(sql);
			dbc.setInt(1, id);
			ResultSet rs = dbc.executeQuery();
			if (rs.next()) {
				uploadFile = new UploadFile();
				fill(rs, uploadFile);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (dbc != null)
					dbc.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return uploadFile;
	}

	public static int remove(int id) {
		int result = 0;
		DBConnect dbc = null;
		String sql = "delete from tb_upload where id =?";

		try {
			dbc = new DBConnect(sql);

			dbc.setInt(1, id);
			dbc.executeUpdate();
			result = 1;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (dbc != null)
					dbc.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/* oracle dao
	public static int addUploadFile(UploadFile uploadFile) {
		int result = 0;
		DBConnect dbc = null;
		String sql = "insert into tb_upload (id,name,urlPath,realPath,uploadUser,uploadDate,remark) values (seq_document.nextval,?,?,?,?,sysdate,?)";

		try {
			dbc = new DBConnect(sql);
			dbc.setString(1, uploadFile.getName());
			dbc.setString(2, uploadFile.getUrlPath());
			dbc.setString(3, uploadFile.getRealPath());
			dbc.setString(4, uploadFile.getUploadUser());
			dbc.setString(5, uploadFile.getRemark());
			dbc.executeUpdate();
			result = 1;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (dbc != null)
					dbc.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;

	}
	*/
	// mysql dao
	public static int addUploadFile(UploadFile uploadFile) {
		int result = 0;
		DBConnect dbc = null;
		String sql = "insert into tb_upload (name,urlPath,realPath,uploadUser,uploadDate,remark,isHomepageVideo) values (?,?,?,?,?,?,?)";

		try {
			dbc = new DBConnect(sql);
			dbc.setString(1, uploadFile.getName());
			dbc.setString(2, uploadFile.getUrlPath());
			dbc.setString(3, uploadFile.getRealPath());
			dbc.setString(4, uploadFile.getUploadUser());
			dbc.setDate(5, new Date(new java.util.Date().getTime()));
			dbc.setString(6, uploadFile.getRemark());
			dbc.setBoolean(7, uploadFile.isHomepageVideo());
			dbc.executeUpdate();
			result = 1;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (dbc != null)
					dbc.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;

	}
}
